package com.ruoyi.order.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.order.domain.VerCart;
import com.ruoyi.order.domain.VerCartDto;
import com.ruoyi.order.domain.VerCartItem;
import com.ruoyi.order.domain.VerCartItemSimple;
import com.ruoyi.order.domain.dto.SkuCartItemDto;
import com.ruoyi.order.mapper.VerCartMapper;
import com.ruoyi.order.service.IVerCartService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 购物车Controller
 *
 * @author ruoyi
 * @date 2025-08-21
 */
@RestController
@Tag(name = "购物车接口", description = "提供购物车操作")
@RequestMapping("/order/cart")
public class VerCartController extends BaseController {
    @Autowired
    private IVerCartService verCartService;

    @Autowired
    private VerCartMapper verCartMapper;

    /**
     * 查询购物车列表
     */
    @PreAuthorize("@ss.hasPermi('order:cart:list')")
    @Operation(summary = "查询购物车列表", description = "传入购物车查询对象，返回购物车列表")
    @PostMapping("/list")
    public TableDataInfo list(VerCart verCart) {
        startPage();
        List<VerCart> list = verCartService.selectVerCartList(verCart);
        return getDataTable(list);
    }

    /**
     * 导出购物车列表
     */
    @PreAuthorize("@ss.hasPermi('order:cart:export')")
    @Log(title = "购物车", businessType = BusinessType.EXPORT)
    @Operation(summary = "导出购物车列表", description = "传入购物车查询对象，返回购物车列表")
    @PostMapping("/export")
    public void export(HttpServletResponse response, VerCart verCart) {
        List<VerCart> list = verCartService.selectVerCartList(verCart);
        ExcelUtil<VerCart> util = new ExcelUtil<VerCart>(VerCart.class);
        util.exportExcel(response, list, "购物车数据");
    }

    /**
     * 获取购物车详细信息
     */
    @PreAuthorize("@ss.hasPermi('order:cart:query')")
    @Operation(summary = "获取购物车详细信息", description = "传入购物车 ID，返回购物车详细信息")
    @PostMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(verCartService.selectVerCartById(id));
    }

    /**
     * 新增购物车
     */
    @PreAuthorize("@ss.hasPermi('order:cart:add')")
    @Operation(summary = "新增购物车", description = "传入skuId、userId、quantity，返回新增结果")
    @Log(title = "购物车", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestParam Long skuId, @RequestParam Long cartId, @RequestParam Integer quantity) {
        VerCartDto verCartDto = new VerCartDto();
        verCartDto.setCartId(cartId);
        verCartDto.setProductSkuId(skuId);
        verCartDto.setQuantity(quantity);
        verCartDto.setItemStatus(0);
        return toAjax(verCartService.insertVerCart(verCartDto));
    }

    /**
     * 修改购物车
     */
    @PreAuthorize("@ss.hasPermi('order:cart:edit')")
    @Operation(summary = "修改购物车", description = "传入购物车对象，返回修改结果")
    @Log(title = "购物车", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody VerCart verCart) {
        return toAjax(verCartService.updateVerCart(verCart));
    }

    /**
     * 删除购物车
     */
    @PreAuthorize("@ss.hasPermi('order:cart:remove')")
    @Operation(summary = "删除购物车", description = "传入购物车 ID，返回删除结果")
    @Log(title = "购物车", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(verCartService.deleteVerCartByIds(ids));
    }


    /**
     * 根据用户id获取简化的购物车列表
     */
    @PreAuthorize("@ss.hasPermi('order:cart:list')")
    @Operation(summary = "根据用户id获取简化的购物车列表", description = "传入用户id，返回简化的购物车列表")
    @PostMapping("/userId")
    public AjaxResult listByUserId(@RequestParam Long userId) {
        List<VerCartItemSimple> grouped = verCartService.listGroupedByUserId(userId);
        return success(grouped);
    }

    /**
     * 添加商品到购物车子项
     */
    @PreAuthorize("@ss.hasPermi('order:cartItem:add')")
    @Operation(summary = "添加商品到购物车子项", description = "传入购物车对象，返回添加结果")
    @Log(title = "购物车", businessType = BusinessType.INSERT)
    @PostMapping("/orderItem/add")
    public AjaxResult addCartItem(@RequestParam Long productSkuId, @RequestParam Integer quantity,
                                  @RequestParam Long userId, @RequestParam(required = false) Long activityId,
                                  @RequestParam(required = false) Long couponId, @RequestParam Integer protection) {
        VerCartItem cartItem = new VerCartItem();
        cartItem.setProductSkuId(productSkuId);
        cartItem.setQuantity(quantity);
        cartItem.setUserId(userId);
        cartItem.setItemStatus(0);
        cartItem.setActivityId(activityId);
        cartItem.setCouponId(couponId);
        cartItem.setProtection(protection);
        return toAjax(verCartService.insertVerCartItem(cartItem));
    }

    /**
     * 返回当前用户所有的购物车子项
     */
    @PreAuthorize("@ss.hasPermi('order:cartItem:list')")
    @Operation(summary = "返回当前用户所有的购物车子项", description = "传入用户id，返回购物车子项列表")
    @PostMapping("/getCartItem/{userId}")
    public AjaxResult getCartItem(@PathVariable Long userId) {
        List<SkuCartItemDto> cartItems = verCartService.getCartItem(userId);
        return success(cartItems);
    }

    /**
     * 逻辑删除购物车子项
     */
    @PreAuthorize("@ss.hasPermi('order:cartItem:remove')")
    @Operation(summary = "逻辑删除购物车子项", description = "传入购物车子项id，返回删除结果")
    @PostMapping("/remove/{cartItemId}")
    public AjaxResult removeCartItem(@PathVariable Long cartItemId) {
        Long delete = verCartMapper.logicalDelete(cartItemId);
        return success(delete);
    }


}
